var loginName = getParam("loginName")||'';
$(function(){
	$("#loginName").text(loginName);
	if(!loginName){
		$(".sucmsg").hide();
        $("#errorText").text("当前用户登录失效").css("color","red");
        $(".errormsg").show();
        return false;
	}
	//点击获取验证码
    $('.safe-code').click(function(){
    	send();
    	sendVertifyCode();
        return false;
    });
	
});
/*设置全局变量sendTime */
var sentTime= 119;
/* 发送验证码 */
function send() {
	if(sentTime == 0) {
		$('.safe-code').removeAttr('disabled');
		$('.safe-code').val('发送验证码');
		sentTime=199;
	}else {
		$('.safe-code').attr('disabled',true);
		$('.safe-code').val('重发验证码（'+sentTime+'）');
		$('.safe-code').css("color","#999");
		sentTime--;
		setTimeout(function(){send();},1000);
	}
}
//发送邮箱验证码
function sendVertifyCode() {
	var email = $("#email").val()||'';
	var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
	if(!loginName){
		$(".sucmsg").hide();
        $("#errorText").text("当前用户登录失效").css("color","red");
        $(".errormsg").show();
        return false;
	}
	//邮箱合法性检查
	if(!email){
		$(".sucmsg").hide();
        $("#errorText").text("请输入绑定邮箱").css("color","red");
        $(".errormsg").show();
        return false;
	}
    if(!reg.test(email)){
		$(".sucmsg").hide();
        $("#errorText").text("请注意邮箱格式").css("color","red");
        $(".errormsg").show();
        return false;
    }
	$.ajax({
    	url: "/common/sendEmailVerifyCode.do?num=6&minute=15",
    	data: {
    		"email":email,
    		"loginName":loginName
    	},
    	dataType: "json",
    	success: function (data) {
    		if(data&&data.returnFlag == "success"){
    			$(".sucmsg").show();
    			$(".errormsg").hide();
    		}else{
    			$(".sucmsg").hide();
    	        $("#errorText").text(data.msg).css("color","red");
    	        $(".errormsg").show();
    		}
    	}
    });
}
//表单是否应提交标识，默认为false 
var isCommitted = false;  
//点击确定按钮
$(".submitButton").click(function(){
	var emailCode = $("#emailCode").val()||"";
	var email = $("#email").val()||"";
	var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
	//获取上一个页面传递过来的参数
	$("#userName").val(loginName);
    if(emailCode == ""){
        $("#errorText").text("请填写验证码").css("color","red");
        $(".errormsg").show();
        return false;
    }
    if(emailCode.length!=6){
        $("#errorText").text("验证码输入有误").css("color","red");
        $(".errormsg").show();
        return false;
    }
    if(!reg.test(email)){
		$(".sucmsg").hide();
        $("#errorText").text("邮箱格式有误").css("color","red");
        $(".errormsg").show();
        return false;
    }
    //验证邮箱验证码
    $.ajax({
        url: "/common/checkEmailVerifyCode.do",
        //type: "post",
        data:{
            "emailCode":emailCode,
            "email":email
        },
        dataType: "json",
        success: function (data) {
            if(data.returnFlag=='fail'){
    	        $("#errorText").text(data.msg).css("color","red");
    	        $(".errormsg").show()
                return false;
            }else {
    			$(".sucmsg").hide();
    	        $(".errormsg").hide();
		        //提交表单到后台
		        if(isCommitted == false){ //提交表单后，将表单是否已经提交设置为true
		        	isCommitted = true; //返回true让表单正常提交
		        	$('#editEmailForm').submit();
		        }
			}
        }
    });
})
